<?php
class BaseAction extends Action {
	
	public function __construct() {
		parent::__construct();
	}
	
	/* 初始化  */
	public function _initialize() {
		
	}
	
	/**
	 * 
	 * 设置用户cookie
	 */
	public function setUserCookie($name, $value, $path = '/', $expire = 3600) {
		cookie($name, $value, array('expire' => $expire, 'path' => $path));
	}
	
	/**
	 * 
	 * 获取用户cookie
	 */
	public function getUserCookie($name, $def = null) {
		$value =  cookie($name);
		if ($value === null) {
			return $def;
		}
		else {
			return $value;
		}
	}
	
	/**
	 * 
	 * 清楚用户cookie
	 */
	public function clearUserCookie($name) {
		cookie($name, null);
	}
	
	/**
	 * 
	 * 设置session
	 */
	public function setUserSession($name, $value) {
		session($name, $value);
	}
	
	/**
	 * 
	 * 获取session
	 */
	public function getUserSession($name) {
		return session($name);
	}
	
	/**
	 * 
	 * 清楚session
	 */
	public function clearUserSession($name) {
		session($name, null);
	}
	
	/**
	 * 
	 * 是否为post请求
	 */
	public function isPost() {
		return $_SERVER["REQUEST_METHOD"] == "POST";
	}
	
	/**
	 * 
	 * 是否为ajax请求
	 */
	public function isAjax() {
		if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == "xmlhttprequest") return true;
		return false;
	}
	
	/**
	 * 
	 * 获取输入参数
	 */
	public function getParameter($key, $def = null) {
		return isset($this->_parameters[$key])?$this->_parameters[$key]:$def;
	}
	
	public function outputJson($array) {
		$json = json_encode($array);
		$this->output($json);
	}
	
	/**
	 * 
	 * 输出字符串
	 */
	public function outputStr($string) {
		echo $string;
		exit();
	}
	
	/**
	 * 
	 * 获取客户端IP地址
	 */
	public function getClientIp() {
		import('ORG.Net.IpLocation');
		return get_client_ip();
	}
}